fix(cast): delete pruned tool-call parts in reverse to avoid index shift#364
Open
Osamaali313 wants to merge 1 commit into
Open
fix(cast): delete pruned tool-call parts in reverse to avoid index shift#364Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
NewBodyPair collects the indices of ToolCall parts with a nil FunctionCall into partsToDelete, then deletes them in a forward loop using those pre-collected ascending indices. Each deletion shifts every later element left by one, so with two or more collected indices the second and subsequent deletions remove the wrong elements: a valid TextContent can be dropped while an invalid nil-FunctionCall tool call survives into the message chain (feeding malformed messages downstream). Iterate the collected indices in reverse so earlier indices stay valid during deletion. Adds a regression test covering two leading nil-FunctionCall tool calls followed by a text part.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
NewBodyPair(backend/pkg/cast/chain_ast.go) prunesToolCallparts that have anilFunctionCallfrom an AI message. It first collects their indices intopartsToDelete(ascending), then deletes them in a forward loop:Each
append-based deletion shifts every later element left by one, but the loop keeps using the original, now-stale indices. With two or more collected indices, the second (and later) deletions remove the wrong elements.Concrete failure
Input parts
[ToolCall{FC:nil}, ToolCall{FC:nil}, TextContent{"keep me"}](two leading invalid tool calls, then real text):["keep me"][ToolCall{FC:nil}]— the legitimate text is dropped and an invalid nil-FunctionCalltool call survives into the message chain, feeding malformed messages to downstream summarization / LLM code.(The scan
continues over nil-FC tool calls and onlybreaks on a valid tool call, so 2+ leading nil-FC tool calls are collected — the trigger.)Fix
Delete the collected indices in reverse, so removing a later element never invalidates the indices still pending:
Tests
Added
TestNewBodyPair_DropsMultipleNilFunctionCallToolCallsinbackend/pkg/cast/newbodypair_reg_test.go.ToolCall, text lost).go test ./pkg/cast/ ./pkg/csum/passes (no regressions) andgo vet ./pkg/cast/is clean.